package com.rongxue.service.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

public class UtilsGraphics2D {
    
    private static final int WIDTH = 500;//图片宽度
    private static final int HEIGHT = 500;//图片高度
    private static String centerName = "电子合同章";
    private static String year = "2016年06月23日";



    public static void main(String[] args) throws Exception{
      BufferedImage image = startGraphics2D("游戏有限公司");

//        BufferedImage image = getSquarePng("啊啊啊");
        try {
            String filePath = "D:/pdf/"+new Date().getTime()+".png";
            ImageIO.write(image, "png", new File(filePath)); //将其保存在C:\\Users\\huage\\Desktop\\121231\\下
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }


    public static String createSeal(String message,Integer type){

        System.out.println("message:"+message);
        BufferedImage image = null;
        String filePath = null;
        try {
            // type 1个人章 2 公章
            if (type==1){
                image = getSquarePng(message);
            }
            if(type==2) {
                image = startGraphics2D(message);
            }
            filePath = FxqUtils.jksSavePath+"/"+new Date().getTime()+".png";
            ImageIO.write(image, "png", new File(filePath));
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

        return filePath;
    }

    /**
     * 圆形公章
     * @param message
     * @return
     */
    public static BufferedImage startGraphics2D(String message){
        // 定义图像buffer
        BufferedImage buffImg = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = buffImg.createGraphics();

        // 增加下面代码使得背景透明
        buffImg = g.getDeviceConfiguration().createCompatibleImage(WIDTH, HEIGHT, Transparency.TRANSLUCENT);
        g.dispose();
        g = buffImg.createGraphics();
        g.setColor(Color.RED);
        BasicStroke stroke = new BasicStroke(5);
        g.setStroke(stroke);

        //设置锯齿圆滑
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        //绘制圆
        int radius = HEIGHT/3;//周半径
        int CENTERX = WIDTH/2;//画图所出位置
        int CENTERY = HEIGHT/2;//画图所处位置


        Ellipse2D circle = new Ellipse2D.Double();
        circle.setFrameFromCenter(CENTERX, CENTERY, CENTERX + radius, CENTERY + radius);
        g.draw(circle);

        //绘制中间的五角星
        g.setFont(new Font("宋体", Font.BOLD, 120));
        g.drawString("★", CENTERX-(120/2), CENTERY+(120/3));

        //添加姓名
//        g.setFont(new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 40));// 写入签名
        g.setFont(new Font("宋体", Font.BOLD, 40));
        g.drawString(centerName, CENTERX -(95), CENTERY +90);

//        //添加年份
//        g.setFont(new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 20));// 写入签名
//        g.drawString(year, CENTERX -(60), CENTERY +(30+80));


        //根据输入字符串得到字符数组
        String[] messages2 = message.split("");
        String[] messages = messages2;
//        String[] messages = new String[messages2.length-1];
//        System.arraycopy(messages2,0,messages,0,messages2.length-1);

        //输入的字数
        int ilength = messages.length;

        //设置字体属性
        int fontsize = 50;
        Font f = new Font("Serif", Font.BOLD, fontsize);

        FontRenderContext context = g.getFontRenderContext();
        Rectangle2D bounds = f.getStringBounds(message, context);

        //字符宽度＝字符串长度/字符数
        double char_interval = (bounds.getWidth() / ilength);
        //上坡度
        double ascent = -bounds.getY();

        int first = 0,second = 0;
        boolean odd = false;
        if (ilength%2 == 1)
        {
            first = (ilength-1)/2;
            odd = true;
        }
        else
        {
            first = (ilength)/2-1;
            second = (ilength)/2;
            odd = false;
        }

        double radius2 = radius - ascent;
        double x0 = CENTERX;
        double y0 = CENTERY - radius + ascent;
        //旋转角度
        double a = 2*Math.asin(char_interval/(2*radius2));

        if (odd)
        {
            g.setFont(f);
            g.drawString(messages[first], (float)(x0 - char_interval/2), (float)y0);

            //中心点的右边
            for (int i=first+1;i<ilength;i++)
            {
                double aa = (i - first) * a;
                double ax = radius2 * Math.sin(aa);
                double ay = radius2 - radius2 * Math.cos(aa);
                AffineTransform transform = AffineTransform.getRotateInstance(aa);//,x0 + ax, y0 + ay);
                Font f2 = f.deriveFont(transform);
                g.setFont(f2);
                g.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa)));
            }
            //中心点的左边
            for (int i=first-1;i>-1;i--)
            {
                double aa = (first - i) * a;
                double ax = radius2 * Math.sin(aa);
                double ay = radius2 - radius2 * Math.cos(aa);
                AffineTransform transform = AffineTransform.getRotateInstance(-aa);//,x0 + ax, y0 + ay);
                Font f2 = f.deriveFont(transform);
                g.setFont(f2);
                g.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa)));
            }

        }
        else
        {
            //中心点的右边
            for (int i=second;i<ilength;i++)
            {
                double aa = (i - second + 0.5) * a;
                double ax = radius2 * Math.sin(aa);
                double ay = radius2 - radius2 * Math.cos(aa);
                AffineTransform transform = AffineTransform.getRotateInstance(aa);//,x0 + ax, y0 + ay);
                Font f2 = f.deriveFont(transform);
                g.setFont(f2);
                g.drawString(messages[i], (float)(x0 + ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay - char_interval/2* Math.sin(aa)));
            }

            //中心点的左边
            for (int i=first;i>-1;i--)
            {
                double aa = (first - i + 0.5) * a;
                double ax = radius2 * Math.sin(aa);
                double ay = radius2 - radius2 * Math.cos(aa);
                AffineTransform transform = AffineTransform.getRotateInstance(-aa);//,x0 + ax, y0 + ay);
                Font f2 = f.deriveFont(transform);
                g.setFont(f2);
                g.drawString(messages[i], (float)(x0 - ax - char_interval/2* Math.cos(aa)), (float)(y0 + ay + char_interval/2* Math.sin(aa)));
            }
        }

        return buffImg;
    }


    /**
     * 方形章个人章
     * @param message
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static BufferedImage getSquarePng(String message)

            throws FileNotFoundException, IOException {


        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);;
        if (message != null) {


            Graphics2D g2 = image.createGraphics();// 得到图形上下文
            image = g2.getDeviceConfiguration().createCompatibleImage(WIDTH, HEIGHT, Transparency.TRANSLUCENT);
            g2.dispose();
            g2 = image.createGraphics();

            BasicStroke stroke = new BasicStroke(5);
            g2.setStroke(stroke);

            g2.setColor(Color.RED); // 设置画笔颜色
            double leftX=WIDTH/6;
            double topY=HEIGHT/6;
            double width=WIDTH*2/3;
            double height=HEIGHT*2/3;

            Rectangle2D rect=new Rectangle2D.Double(leftX,topY,width,height);
            g2.draw(rect);

            // 设置字体

//            g2.setFont(new Font("宋体", Font.LAYOUT_LEFT_TO_RIGHT, 155));// 写入签名
            g2.setFont(new Font("宋体", Font.BOLD, 155));// 写入签名

            if (message != null) {

                String newMessage = null;

        // 如果三个 则 XXX章 如果两个则 XX之章 大于3个 则 取前三

                if (message.length() == 2) {

                    newMessage = message + "之章";

                } else if (message.length() == 3) {

                    newMessage = message + "章";

                } else if (message.length() > 3) {

                    newMessage = message.substring(0, 3) + "章";

                }

                char[] charArray = newMessage.toCharArray();

                g2.drawString(new String(new char[] { charArray[0] }), 255, 220);  // 写文字

                g2.drawString(new String(new char[] { charArray[1] }), 255, 380); // 写文字

                g2.drawString(new String(new char[] { charArray[2] }), 95, 220);  // 写文字

                g2.drawString(new String(new char[] { charArray[3] }), 95, 380); // 写文字

            }

        }

        return image;

    }

}